package test;

import java.util.concurrent.TimeUnit;

/**
 * Created with IntelliJ IDEA.
 *
 * @ project name : example
 * @ Author: XuLeHuang
 * @ Date: 2022/11/7 22:00
 * @ Description:
 */
public class ClassMonitor {
    public static synchronized void method1() {
        System.out.println(Thread.currentThread().getName() + "-----获取锁" + "----执行方法1");
        try {
            TimeUnit.MINUTES.sleep(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public static synchronized void method2() {
        System.out.println(Thread.currentThread().getName() + "-----获取锁" + "----执行方法2");
        try {
            TimeUnit.MINUTES.sleep(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        ClassMonitor classMonitor1 = new ClassMonitor();
        ClassMonitor classMonitor2 = new ClassMonitor();
        Thread thread1 = new Thread(new Runnable() {
            @Override
            public void run() {
                classMonitor1.method1();
            }
        }, "thread1");
        Thread thread2 = new Thread(new Runnable() {
            @Override
            public void run() {
                classMonitor2.method2();
            }
        }, "thread2");

        thread1.start();
        thread2.start();

        while (true) {
            try {
                TimeUnit.SECONDS.sleep(2);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(thread1.getName() + "状态----" + thread1.getState());
            System.out.println(thread2.getName() + "状态----" + thread2.getState());
        }
    }
}
